﻿/************************************************************************

 This file is part of Chroma.

 EscapeFromPrisonPlanet is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 EscapeFromPrisonPlanet is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2013 Scott Bevin. all rights reserved

using Microsoft.Xna.Framework;

namespace LudumDare26.PrimitiveRendering
{
	public class Vector2Ext
	{
		/// <summary>
		/// Finds the normal from two vectors
		/// </summary>
		/// <param name="first">The first vector</param>
		/// <param name="second">The second vector</param>
		/// <returns></returns>
		public static Vector2 FindEdgeNormal(Vector2 first, Vector2 second)
		{
			//Xbox360 need this variable to be initialized to Vector2.Zero
			Vector2 normal = Vector2.Zero;

			Vector2 temp = new Vector2(first.X - second.X, first.Y - second.Y);

			normal.X = -temp.Y; // get 2D normal
			normal.Y = temp.X; // works only on counter clockwise polygons

			normal.Normalize();

			return normal;
		}

		public static Vector2 FindVertexNormal(Vector2 first, Vector2 second, Vector2 third)
		{
			Vector2 temp;
			Vector2 one = FindEdgeNormal(first, second);
			Vector2 two = FindEdgeNormal(second, third);

			Vector2.Add(ref one, ref two, out temp);
			temp.Normalize();
			return temp;
		}
	}
}